#include "fig_ui_pausemenu.h"
#include "gen_game_state.h"
#include "fig_sounds.h"
#include "fig_ios_wrapper.h"
#include "fig_vars.h"
#include "fig_constants.h"
#include "fig_player_craft.h"
#include "fig_ui_removead.h"

#include "ui/ui_manager.h"
#include "gameflow/game_flow_manager.h"
#include "gameflow/ui_popup.h"
#include "gameflow/ui_state.h"
#include "font/font_manager.h"
#include "graphics/graphics.h"
#include "graphics/graphics_util.h"
#include "application/game_application.h"

#include "util/log.h"
#include "util/profiler.h"

#ifdef _PLATFORM_ANDROID
#include "SDL2/src/core/android/SDL_android.h"
#include "android/sleipner_android.h"
#endif

using namespace gameflow;

std::string FIGUIPauseMenu::m_StaticFileName = "";


FIGUIPauseMenu&     FIGUIPauseMenu::Instance()
					
{
    float AspectRation = (float)( (float)GetGraphics()->GetScreenHeight()  / (float)GetGraphics()->GetScreenWidth());
    
	if(AspectRation >= 1.65f)
	{
		m_StaticFileName = "ui/5_pause_menu.ui";
		
	}
	else if(AspectRation <= 1.34 )
	{
		m_StaticFileName = "ui/ipad_pause_menu.ui";
		
	}
	else
	{
		m_StaticFileName = "ui/pause_menu.ui";
		
	}
    
	static FIGUIPauseMenu	Inst;
	return Inst;
}


void	FIGUIPauseMenu::Tick(float dt)
{
	if( m_ForceDisableTimer > 0)
		m_ForceDisableTimer -=  dt;

	FIGUIPopupAnimator::Tick(dt);
}


FIGUIPauseMenu::FIGUIPauseMenu() :
	FIGUIPopupAnimator (m_StaticFileName, ""),
    m_FoceDisable(false),
	m_ForceDisableTimer(0)
{

	SetCloseButton("Button_Resume");
   
	AddToNaviationUI(m_pPopup->GetWidget("Button_Resume"));
	AddToNaviationUI(m_pPopup->GetWidget("Button_GoToMainMenu"));
	AddToNaviationUI(m_pPopup->GetWidget("SETTINGS"));
	
	
	
	m_pPopup->SetWidgetFunctor("Button_GoToMainMenu",UI_DELEGATE(FIGUIPauseMenu, OnButtonGotoMainMenu));
	m_pPopup->SetWidgetFunctor("play_button",UI_DELEGATE(FIGUIPauseMenu, OnButtonResume));
	m_pPopup->SetWidgetFunctor("Button_Resume",UI_DELEGATE(FIGUIPauseMenu, OnButtonResume));
	

	m_pPopup->SetWidgetFunctor("toFB",UI_DELEGATE(FIGUIPauseMenu, OnButtonGotoFacebook));
	m_pPopup->SetWidgetFunctor("toTwitter",UI_DELEGATE(FIGUIPauseMenu, OnButtonGotoTwitter));
	m_pPopup->SetWidgetFunctor("to2DStar",UI_DELEGATE(FIGUIPauseMenu, OnButtonGoto2DStar));
	m_pPopup->SetWidgetFunctor("SETTINGS",UI_DELEGATE(FIGUIPauseMenu, OnButtonSettings));
	
	m_pPopup->SetupTransition("goto_main_menu", "MainMenu");
	//m_pPopup->SetWidgetPopup("SETTINGS", "settings");
	//SetWidgetFunctor("Button_RemoveAd",UI_DELEGATE(FIGUIPauseMenu, OnButtonFullVersion));
    ui::UIWidget* pWidget = m_pPopup->GetWidget("Button_RemoveAd");
    if(pWidget)
    {
        pWidget->SetIsVisible(false);
    }
    
    if(m_pPopup->GetWidget("play_button"))
        m_pPopup->GetWidget("play_button")->SetSize(m_pPopup->GetWidget("play_button")->GetSize() * 1.3f);

	m_Type = ANIMATION_MSGPOPUP;
	FIGUIPopupAnimatorMgr::Instance().AddPopupAnimator(FIGUIPopupAnimatorMgr::POPUP_NAMES_PAUSEMENU,this);
    
    
//#ifdef _PLATFORM_IOS
    if(m_pPopup->GetWidget("link_buttons"))
        m_pPopup->GetWidget("link_buttons")->SetPosition(math::Vec2(3.0f,3.0f));
//#endif

}

void DELEGATE_CALL	FIGUIPauseMenu::OnButtonSettings(ui::UIWidget::WIDGET_EVENT Event, ui::UIWidget* pWidget)
{
	FIGUIPopupAnimatorMgr::Instance().Show(FIGUIPopupAnimatorMgr::POPUP_NAMES_SETTING);
}

void	FIGUIPauseMenu::Show()
{
    if(m_FoceDisable)
        return;

	if(m_ForceDisableTimer > 0)
	{
		Resume();
		m_ForceDisableTimer = 0;
		return;
	}
    
	RefreshUI();
	FIGUIPopupAnimator::Show();
	GENGameState::GetGame()->Pause();
	audio::BassAudio::Instance().PauseAll();
}

void	FIGUIPauseMenu::Hide()
{
    FIGUIPopupAnimator::Hide();
	GENGameState::GetGame()->UnPause();
	audio::BassAudio::Instance().RestartAll();

	if( GameRMS::Instance()->IsTiltEnabled())
	{
		input::Joystick::Instance().JoystickEnable();
		FIGPlayerCraft::Instance().MarkGyroscope();
	}

}

void DELEGATE_CALL		FIGUIPauseMenu::OnButtonGotoMainMenu(ui::UIWidget::WIDGET_EVENT Event, ui::UIWidget* pWidget)
{
	Hide();
	GENGameState::GetGame()->Pause();
	audio::BassAudio::Instance().PauseAll();

	FIGUIPopupAnimatorMgr::Instance().Show(FIGUIPopupAnimatorMgr::POPUP_NAMES_YESNO);
	gameflow::UIPopupManager::Instance().StartYesNo("LOC_ARE_YOU_SURE", UI_DELEGATE(FIGUIPauseMenu, OnYesGoToMainMenu), UI_DELEGATE(FIGUIPauseMenu, OnButtonResume));		
}

void DELEGATE_CALL		FIGUIPauseMenu::OnYesGoToMainMenu(ui::UIWidget::WIDGET_EVENT Event, ui::UIWidget* pWidget)
{
	//GENGameState::GetGame()->UnPause();

	FIGPlayerCraft::Instance().ResetToDefault();

	FIGVars::Instance()->Save();

	ui::UIWidget* pWidget2 = m_pPopup->GetWidget("goto_main_menu");
	if(pWidget2)
		pWidget2->OnMouseRelease(pWidget2->GetPosition(),input::Mouse::BUTTON_LEFT);


}


void DELEGATE_CALL		FIGUIPauseMenu::OnButtonResume(ui::UIWidget::WIDGET_EVENT Event, ui::UIWidget* pWidget)
{
	Resume();		
}

void	FIGUIPauseMenu::Resume()
{
	if(GENGameState::GetGame())
		GENGameState::GetGame()->UnPause();
	audio::BassAudio::Instance().RestartAll();

	
	this->Hide();
}

void DELEGATE_CALL		FIGUIPauseMenu::OnButtonGotoFacebook(ui::UIWidget::WIDGET_EVENT Event, ui::UIWidget* pWidget)
{

    FIGIOSWrapper::Instance().FIG_GotoWebSite("http://www.facebook.com/2dstar");

    // Testing
#ifdef _PLATFORM_ANDROID
    Android_JNI_SendMessage(FUNC_gotoURL, 2);
#endif

}

void DELEGATE_CALL		FIGUIPauseMenu::OnButtonGotoTwitter(ui::UIWidget::WIDGET_EVENT Event, ui::UIWidget* pWidget)
{

    FIGIOSWrapper::Instance().FIG_GotoWebSite("http://www.twitter.com/2dstar");
#ifdef _PLATFORM_ANDROID
    Android_JNI_SendMessage(FUNC_gotoURL, 3);
#endif

}
	
void DELEGATE_CALL		FIGUIPauseMenu::OnButtonGoto2DStar(ui::UIWidget::WIDGET_EVENT Event, ui::UIWidget* pWidget)
{

    FIGIOSWrapper::Instance().FIG_GotoWebSite("http://www.2dstar.com");
#ifdef _PLATFORM_ANDROID
    Android_JNI_SendMessage(FUNC_gotoURL, 4);
#endif

}

void DELEGATE_CALL		FIGUIPauseMenu::OnButtonFullVersion(ui::UIWidget::WIDGET_EVENT Event, ui::UIWidget* pWidget)
{
	FIGUIRemoveAd::Instance().Show(NULL);
}


void		FIGUIPauseMenu::RefreshUI()
{
	if(FIGVars::Instance()->GetInt(FIGVars::VAR_NAMES_IS_FULL_VERSION))
	{
		ui::UIWidget* pWidget = m_pPopup->GetWidget("Button_RemoveAd");			
		if(pWidget)
		{
			pWidget->SetIsVisible(false);
		}
	}
}

void		FIGUIPauseMenu::Toggle()
{
	if(m_pPopup->IsActive())
	{
		Hide();
	}
	else
	{
		Show();
	}
}

//void		FIGUIPauseMenu::KeyPressed(input::Keyboard::KEY* pKey)
//{
//
//	switch(pKey->KeyID)
//	{
//		case KEY_ESCAPE:
//		{
//			Resume();				
//		}
//		break;
//	}
//	
//}

